//
//  SearchResult.swift
//  shop
//
//  Created by chen on 2025/5/17.
//

import SwiftUI

struct SearchResult: View {
    
    var keyWord: String
    
    @StateObject private var vm: SearchResultViewModel = SearchResultViewModel()
    
    @EnvironmentObject private var router: Router
    
    @State private var selectedTab: Int = 0
    
    var body: some View {
        ZStack{
            Color.detailBg
                .ignoresSafeArea()
            
            VStack(spacing: 0) {
                // 顶部工具栏
                SearchHeader(keyWord: keyWord)
                
                // Tab 选项卡
                HStack(spacing: 0) {
                    ForEach(vm.sortType, id: \.type) { item in
                        HStack(spacing: 0){
                            Text("\(item.name)")
                                .font(.system(size: 16))
                                .foregroundColor(selectedTab == item.type ? .red : .black)
                                .frame(height: 40)
                                .id(item.type)
                                .onTapGesture {
                                    withAnimation {
                                        selectedTab = item.type
                                    }
                                }
                        }
                        .frame(maxWidth: .infinity)
                    }
                }
                .frame(maxWidth: .infinity)
                
                TabView(selection: $selectedTab) {
                    ForEach(vm.sortType, id: \.type) { item in
                        ScrollView(showsIndicators: false){
                            LazyVStack{
                                if let list = vm.searchMap[item.type], !list.isEmpty {
                                    ForEach(list, id: \.goodsId) { item in
                                        Button {
                                            router.to(.detailView(id: item.id))
                                        } label: {
                                            SearchResultCard(item: item)
                                        }
                                        .buttonStyle(PlainButtonStyle())
                                    }
                                } else {
                                    if let loading = vm.loadMap[item.type], !loading {
                                        Text("暂无搜索结果")
                                            .font(.system(size: 15))
                                            .foregroundStyle(.black)
                                    }
                                }
                            }
                            .padding(.bottom, 8)
                            .onAppear{
                                vm.getSearchMap(keyWord: keyWord, type: item.type)
                            }
                            
                            if let loading = vm.loadMap[item.type], loading {
                                ProgressView()
                                    .progressViewStyle(CircularProgressViewStyle(tint: .white))
                            }
                        }
                        .tag(item.type)
                    }
                }
                .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            }
        }
        .navigationBarBackButtonHidden(true)
    }
}

#Preview {
    SearchResult(keyWord: "红米")
}
